package com.mt.roll.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.ReportSheet;
import com.mt.roll.core.dal.entity.ReportSheetCategory;
import com.mt.roll.core.dal.service.ReportSheetCategoryServiceBiz;
import com.mt.roll.core.dal.service.ReportSheetServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.params.ReportSheetCategoryParam;
import com.mt.roll.report.params.ReportSheetCategoryQueryParam;
import com.mt.roll.report.service.ReportSheetCategoryService;
import com.mt.roll.report.vo.ReportSheetCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: liguanghao
 * @Date: 2021/3/25 15:10
 * @Version 1.0
 */@Service
@Transactional
public class ReportSheetCategoryServiceImpl extends BaseService implements ReportSheetCategoryService {
     @Resource
     private ReportSheetCategoryServiceBiz reportSheetCategoryServiceBiz;
     @Resource
     private ReportSheetServiceBiz reportSheetServiceBiz;
    @Override
    public R<ReportSheetCategoryVo> get(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        ReportSheetCategory reportSheetCategory = reportSheetCategoryServiceBiz.getById(idParam.getId());
        ReportSheetCategoryVo reportSheetCategoryVo = ReportSheetCategoryVo.builder().build();
        BeanUtils.copyProperties(reportSheetCategory, reportSheetCategoryVo);
        return successful(reportSheetCategoryVo);
    }

    @Override
    public R<List<ReportSheetCategoryVo>> list(ReportSheetCategoryQueryParam reportSheetCategoryQueryParam) {
        QueryWrapper<ReportSheetCategory> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(reportSheetCategoryQueryParam.getName()), ReportSheetCategory.NAME, reportSheetCategoryQueryParam.getName());
        List<ReportSheetCategory> list = reportSheetCategoryServiceBiz.list(wrapper);
        List<ReportSheetCategoryVo> reportSheetCategoryVos = com.mt.roll.core.utils.BeanUtils.copyList(list, ReportSheetCategoryVo.class);
        for (ReportSheetCategoryVo reportSheetCategoryVo:reportSheetCategoryVos
             ) {
            QueryWrapper<ReportSheet> wrapper1=new QueryWrapper<>();
            wrapper1.eq(ReportSheet.CATEGORY,reportSheetCategoryVo.getId());
            int count = reportSheetServiceBiz.count(wrapper1);
            reportSheetCategoryVo.setQuantity(count);
        }
        return successful(reportSheetCategoryVos);
    }

    @Override
    public R<PageVo<ReportSheetCategoryVo>> page(ReportSheetCategoryQueryParam reportSheetCategoryQueryParam) {
        QueryWrapper<ReportSheetCategory> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(reportSheetCategoryQueryParam.getName()), ReportSheetCategory.NAME, reportSheetCategoryQueryParam.getName());
        wrapper.orderByDesc(ReportSheetCategory.CREATE_TIME);
        Page<ReportSheetCategory> page = reportSheetCategoryServiceBiz.page(new Page<>(reportSheetCategoryQueryParam.getCurrent(), reportSheetCategoryQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, ReportSheetCategoryVo.class));
    }

    @Override
    public R<IdVo> add(ReportSheetCategoryParam reportSheetCategoryParam) {
        QueryWrapper<ReportSheetCategory> wrapper = new QueryWrapper<>();
        wrapper.eq(ReportSheetCategory.NAME, reportSheetCategoryParam.getName());
        Assert.eqZero(reportSheetCategoryServiceBiz.count(wrapper), "名称已存在");
        ReportSheetCategory reportSheetCategory=new ReportSheetCategory();
        BeanUtils.copyProperties(reportSheetCategoryParam, reportSheetCategory);
        setCommonField(reportSheetCategory);
        reportSheetCategoryServiceBiz.save(reportSheetCategory);
        return successful(IdVo.builder().id(reportSheetCategory.getId()).build());
    }

    @Override
    public R<IdVo> update(ReportSheetCategoryParam reportSheetCategoryParam) {
        ReportSheetCategory reportSheetCategory = reportSheetCategoryServiceBiz.getById(reportSheetCategoryParam.getId());
        if (!reportSheetCategory.getName().equals(reportSheetCategory.getName())) {
            QueryWrapper<ReportSheetCategory> wrapper = new QueryWrapper<>();
            wrapper.eq(ReportSheetCategory.NAME, reportSheetCategoryParam.getName());
            Assert.eqZero(reportSheetCategoryServiceBiz.count(wrapper), "名称已存在");
        }
        ReportSheetCategory updateReportSheetCategory = new ReportSheetCategory();
        BeanUtils.copyProperties(reportSheetCategoryParam, updateReportSheetCategory);
        setUpdateCommonField(updateReportSheetCategory);
        reportSheetCategoryServiceBiz.updateById(updateReportSheetCategory);
        return successful(IdVo.builder().id(reportSheetCategory.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        ReportSheetCategory reportSheetCategory = reportSheetCategoryServiceBiz.getById(idParam.getId());
        if (reportSheetCategory == null) {
            return failed("报表不存在");
        }
        reportSheetCategoryServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(reportSheetCategory.getId()).build());
    }
}
